<?php
	//Xay dung tim kiem
	$ssFilter 		= $this->arrParam['ssFilter'];
	$page = '';
	if(isset($this->arrParam['page'])){
		$page = '/page/' . $this->arrParam['page'];
	}
	$tn = 0;
	if(isset($this->arrParam['tn'])){
		$tn = $this->arrParam['tn'];
		$page .= '/tn/' . $this->arrParam['tn'];
	}
	
	$action_link 	= $this->baseUrl($this->currentController . '/filter/type/order/tn/' . $tn);
	$lblName 		= $this->cmsLinkSort('Tên khách hàng', 'kh.name', $ssFilter, $this->imgUrl . '/admin',$action_link);
	$lblStatus 		= $this->cmsLinkSort('Trạng thái', 'kh.status', $ssFilter,$this->imgUrl . '/admin',$action_link);
	$lblQuanly		= $this->cmsLinkSort('Người quản lý', 'quanly_name',  $ssFilter, $this->imgUrl. '/admin', $action_link);	
	$lblPhongban	= $this->cmsLinkSort('Phòng ban', 'name_phongban',  $ssFilter, $this->imgUrl. '/admin', $action_link);	
	$lblCapnhat		= $this->cmsLinkSort('Cập nhật', 'kh.modified_by',  $ssFilter, $this->imgUrl. '/admin', $action_link);
	
	
	//---------------------GROUP SELECTBOX --------------------//
	$this->slbCategory[0] = '- Chọn nhóm -';
	$linkSlbCategory 	= $this->baseUrl($this->currentController . '/filter/type/id_cat/tn/' . $this->arrParam['tn']);
	$selectCategory 	= $this->formSelect('id_cat',$ssFilter['id_cat'],array('onchange'=>"OnSubmitForm('" .$linkSlbCategory ."')"),$this->slbCategory);
	
	$linkSlbNguondk 	= $this->baseUrl($this->currentController . '/filter/type/id_nguon_dk/tn/' . $this->arrParam['tn']);
	$selectNguondk 		= $this->formSelect('id_nguon_dk',$ssFilter['id_nguon_dk'],array('onchange'=>"OnSubmitForm('" .$linkSlbNguondk ."')"),$this->slbNguondk);
	
	$linkSlbPhongban 	= $this->baseUrl($this->currentController . '/filter/type/id_phongban/tn/' . $this->arrParam['tn']);
	$selectPhongban 	= $this->cmsSelect('id_phongban', $ssFilter['id_phongban'], $this->slbPhongban, array('onchange'=>"OnSubmitForm('" .$linkSlbPhongban ."')"));
	
	$linkSlbNhanvien 	= $this->baseUrl($this->currentController . '/filter/type/id_nhanvien/tn/' . $this->arrParam['tn']);
	$selectNhanvien 	= $this->formSelect('id_nhanvien', $ssFilter['id_nhanvien'], array('onchange'=>"OnSubmitForm('" .$linkSlbNhanvien ."')"), $this->slbNhanvien);
	
	$linkSearch = $this->baseUrl($this->currentController . '/filter/type/search/key/1/tn/' . $this->arrParam['tn']);
	$btnSearch = $this->formButton('search-now','Tìm', array('class' => 'btnsearch fr', 'onclick'=>"OnSubmitForm('" .$linkSearch . "')"));
	
	$linkResetSearch = $this->baseUrl($this->currentController . '/filter/type/search/key/0/tn/' . $this->arrParam['tn']);
	$btnResetSearch = $this->formButton('clear-keywords','Xóa',array('class' => 'btnsearch fr', 'onclick'=>"OnSubmitForm('" .$linkResetSearch ."')"));
	
	$keywords = $this->formText('keywords',$ssFilter['keywords'],array('class'=>'txtshort txtsearch','style' => 'width:100px;'));
	
	$tungay = $this->formText('tungay',$ssFilter['tungay'],array('class'=>'txtshort txtsearch hasDatePicker ','style' => 'margin-left:3px; width:70px;','placeholder' => 'Từ ngày'));
	$denngay = $this->formText('denngay',$ssFilter['denngay'],array('class'=>'txtshort txtsearch hasDatePicker','style' => 'width:70px;','placeholder' => 'đến ngày'));
	
	
	$strSearch = $keywords . ' ' . $tungay . ' ' . $denngay . ' ' . $btnSearch . ' ' . $btnResetSearch;

	$strSearchRight = $selectNguondk.' '.$selectPhongban . ' ' . $selectNhanvien . ' ' . $selectCategory;
?>

<div class="left_content">
	<?php echo $this->render($this->arrParam['controller'] . '/toolbar/left.php');?>
</div>

<div class="right_content">
	<div class="appContent">
	
	<form action="" method="post" name="appForm">
		<?php echo $this->render($this->arrParam['controller'] . '/toolbar/index.php');?>
		<?php
			if($this->arrParam['save'] == 'ok'){
				echo '<div class="thongbaoThanhCong goc5">Xử lý dữ liệu thành công</div>';
			} 
		?>
		<div class="formList goc5">
			<div class="appTop goct5">
				<div class="block_filter">
					<div class="fl">
						<div class="listSearch">
							<?php echo $strSearch; ?>
							<div class="clr"></div>			
						</div>
					</div>
					<div class="fr">
						<div class="listSelect">
							<?php echo $strSearchRight; ?>
						</div>
					</div>
					<div class="clr"></div>
				</div>
			</div>
			<div class="appBody">
				<table class="tblList">
					<tr>
						<th width="20"><input type="checkbox" id="checkall" class="checkbox" onclick="checkedAll();" /></th>
						<th><?php echo $lblName; ?></th>
						<th width="110">Điện thoại</th>
						<th width="120">Email</th>
						<th width="120"><?php echo $lblQuanly; ?></th>
						<th width="180"><?php echo $lblPhongban; ?></th>
					<!-- 	<th width="80"><?php // echo $lblCapnhat ?></th> -->
						<th width="70"><a href="javascript:void(0)">Thao tác</a></th>
					</tr>
					
					<?php 
						foreach ($this->Items as $key => $val) {
							$id 			= $val['id'];
							$name 			= $val['name'];
							$phone 			= $val['phone'];
							$email 			= $val['email'];
							$status 		= $val['status'];
							$created 		= $val['created'];
							$category_name	= $val['category_name'];
							$quanly_name	= $val['quanly_name'];
							$name_phongban	= $val['name_phongban'];
							$quanly_alias	= $val['quanly_alias'];
							if($val['quanly_alias'] != '') {
								$quanly_name = $val['quanly_name'] . ' (' . $val['quanly_alias'] . ')';
							}
							$capnhat 		= $val['modified'];
							$code 			= '';
							if(!empty($val['code'])) {
								$code = '<span class="code"> - ' . strtoupper($val['code']) . '</span>';
							}
							
							$linkEdit		= $this->baseUrl('khachhang/admin-khcn/edit/id/' . $id . $page);
							
							if ($val['id_cat'] == 2) {
								$linkEdit		= $this->baseUrl('khachhang/admin-khtc/edit/id/' . $id . $page);
							}
							
							$btnEdit		= $this->cmsIconButton('Sửa', $this->imgUrl . '/admin/icon_edit.png', $linkEdit);
							
							$linkDelete		= $this->baseUrl($this->currentController . '/delete/id/' . $id . $page);
							$btnDelete		= $this->cmsIconButton('Xóa', $this->imgUrl . '/admin/icon_del.png', $linkDelete);
							
							$btnAdd			= $this->cmsIconButton('', $this->imgUrl . '/menu/icon-16-maintenance.png', 'javascript::void(0)', array('onclick' => 'showFunction('. $id .')'));
							
							$linkAddHopdong = $this->baseUrl('hopdong/admin-item/add/id_kh/' . $id);
							$linkHopdong 	= $this->baseUrl('hopdong/admin-item/index/id_kh/' . $id);
							$linkAddCLB 	= $this->baseUrl('club/admin-sinhhoatclb/register/id/' . $id);
							$linkhistory	= $this->baseUrl('khachhang/admin-history/index/id/' . $id);
							$linkclass	 	= $this->baseUrl('khachhang/admin-history/hoctap/id/' . $id);
							
							$liHopdong		= '';
							if(!empty($val['code'])) {
								$liHopdong 	= '<li><a href="' . $linkHopdong . '" class="icon-16-article">Danh sách hợp đồng</a></li>';
							}
							
							$divAdd			= '<div class="blockFunction selectFunction'.$id .'">
													<ul>
														<li><a target="_blank" href="' . $linkAddHopdong . '" class="icon-16-new">Thêm hợp đồng</a></li>
														'.$liHopdong.'
														<li><a target="_blank" href="' . $linkAddCLB . '" class="icon-16-new">Thêm vào CLB</a></li>
														<li><a target="_blank" href="' . $linkhistory . '" class="icon-16-new">Lịch sử chăm sóc KH</a></li>
														<li><a target="_blank" href="' . $linkclass . '" class="icon-16-new">Lịch sử học tập</a></li>
													</ul>
												</div>';
							$strIconButton 	= $btnEdit . ' ' . $btnDelete . ' ' . $btnAdd . $divAdd;
							
							$category_name	= '<p class="smallsub">(' . $val['category_name'] . ' - Ngày thêm mới '.$created.')'.$code.'</p>';
							$name 			= '<b><a href="' . $linkEdit . '">' . $name . '</a></b>' . $category_name;
							
							$cid 			= $this->formCheckbox('cid[]',$val['id']);
					?>
						<tr <?php if (($id % 2) != 0) echo "class='row1'"; ?>>
							<td align="center"><?php echo $cid; ?></td>
							<td align="left"><?php echo $name; ?></td>
							<td align="center"><?php echo $phone; ?></td>
							<td align="left"><?php echo $email; ?></td>
							<td align="center"><?php echo $quanly_name; ?></td>
							<td align="center"><?php echo $name_phongban; ?></td>
							<!-- <td align="center"><?php //echo $capnhat; ?></td> -->
							<td align="center">
								<div style="position:relative">
									<?php echo $strIconButton; ?>
								</div>
							</td>
						</tr>
					<?php
						}
					?>					
				</table>
			</div>
			<div class="appBottom">
				<div class="pagination">
					<?php
						$currentUrl = $this->baseUrl($this->currentController . '/index' . '/tn/' . $tn);
						echo $this->paginationControl($this->paginator, 'Sliding', 'pagination.phtml', array('currentUrl'=>$currentUrl)); 
					?>
				</div>
			</div>
		</div>
	</form>
	</div>
</div>
<div class="clr"></div>	
<script type="text/javascript">
	function showFunction(i){
		$('.selectFunction' + i).show();
		$('body').append('<div id="overlay" style="width:100%; height:100%; top: 0; left: 0; z-index: 499; position: fixed;" onclick="hideOption()"></div>');
	}
	function hideOption(){
		$('.blockFunction').hide();
		$('#overlay').remove();
	}
</script>
